package com.xuexiang.xui_lib.component.progress.ratingbar;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.StackLayout;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.util.ArrayList;
import java.util.List;

/**
 * PartialView
 *
 * @author xuexiang
 * @since 2019-03-26
 */
class PartialView extends StackLayout {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG");
    static final int NUM0 = 0;
    static final int NUM1 = 1;
    static final int NUM2 = 2;
    static final int NUM3 = 3;
    static final int NUM4 = 4;
    static final float NUM0_25 = 0.25f;
    PixelMap filledPixelMap;
    //PixelMap emptyPixelMap;
    ComponentContainer.LayoutConfig params;
    private Image mFilledView;
    private Image mEmptyView;
    private int mStarWidth = 0;
    private int mStarHeight = 0;
    private boolean isScale = false;

    public PartialView(Context context, int partialViewId, int starWidth, int startHeight, int padding) {
        super(context);

        mStarWidth = starWidth;
        mStarHeight = startHeight;

        setTag(partialViewId);
        setPadding(padding, padding, padding, padding);
        init();
    }

    public PartialView(Context context, AttrSet attrs) {
        super(context, attrs);
        init();
    }

    public PartialView(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, "");
        init();
    }

    private void init() {
        params = new ComponentContainer.LayoutConfig();
        params.width = mStarWidth == 0 ? ComponentContainer.LayoutConfig.MATCH_CONTENT : mStarWidth;
        params.height = mStarHeight == 0 ? ComponentContainer.LayoutConfig.MATCH_CONTENT : mStarHeight;
        mFilledView = new Image(getContext());
        mFilledView.setScaleMode(Image.ScaleMode.ZOOM_START);
        addComponent(mFilledView, params);

        mEmptyView = new Image(getContext());
        addComponent(mEmptyView, params);

        setEmpty();
    }

    public boolean isScale() {
        return isScale;
    }

    public void setScale(boolean scale) {
        isScale = scale;
    }

    public void setFilledDrawable(PixelMap drawable) {
        if (drawable == null) {
            return;
        }
        filledPixelMap = drawable;

        mFilledView.setPixelMap(drawable);
    }

    public void setEmptyDrawable(PixelMap drawable) {
        if (drawable == null) {
            return;
        }
        //emptyPixelMap = drawable;
        mEmptyView.setPixelMap(drawable);
    }

    public void setFilled() {
        mFilledView.setPixelMap(getSplitPixMap(filledPixelMap).get(NUM3));
        mFilledView.setVisibility(Component.VISIBLE);
        mEmptyView.setVisibility(Component.HIDE);
    }

    public void setPartialFilled(float rating) {
        HiLog.info(LABEL, "setPartialFilled===>" + rating);
        float percentage = rating % 1;
        mFilledView.setVisibility(Component.VISIBLE);
        mEmptyView.setVisibility(isScale ? Component.HIDE : Component.VISIBLE);
        if (percentage > NUM0_25 * NUM2) {
            mFilledView.setPixelMap(getSplitPixMap(filledPixelMap).get(NUM2));
        } else if (percentage > NUM0_25) {
            mFilledView.setPixelMap(getSplitPixMap(filledPixelMap).get(NUM1));
        } else if (percentage > NUM0) {
            mFilledView.setPixelMap(getSplitPixMap(filledPixelMap).get(NUM0));
        } else {
            if (rating > NUM0) {
                mFilledView.setPixelMap(getSplitPixMap(filledPixelMap).get(NUM3));
            } else {
                mFilledView.setVisibility(Component.HIDE);
            }
        }
    }

    private List<PixelMap> getSplitPixMap(PixelMap pixelMap) {
        List<PixelMap> splitList = new ArrayList<>();
        int pieceWidth = pixelMap.getImageInfo().size.width / NUM4;
        int pieceHeight = pixelMap.getImageInfo().size.height;
        HiLog.info(LABEL, pieceWidth + "/" + pieceHeight);
        for (int temp = 0; temp < NUM3; temp++) {
            PixelMap splitPixelMap = PixelMap.create(pixelMap,
                new Rect(0, 0, pieceWidth * (temp + 1), pieceHeight),
                getInitializationOptions(pieceWidth * (temp + 1), pieceHeight));
            splitList.add(splitPixelMap);
        }
        splitList.add(pixelMap);
        return splitList;
    }

    private PixelMap.InitializationOptions getInitializationOptions(int width, int height) {
        PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
        initializationOptions.size = new Size(width, height);
        initializationOptions.pixelFormat = PixelFormat.ARGB_8888;
        return initializationOptions;
    }

    public void setEmpty() {
        mFilledView.setVisibility(Component.HIDE);
        mEmptyView.setVisibility(Component.VISIBLE);
    }

    public void setStarWidth(int starWidth) {
        mStarWidth = starWidth;

        ComponentContainer.LayoutConfig params = mFilledView.getLayoutConfig();
        params.width = mStarWidth;
        mFilledView.setLayoutConfig(params);
        mEmptyView.setLayoutConfig(params);
    }

    public void setStarHeight(int starHeight) {
        mStarHeight = starHeight;

        ComponentContainer.LayoutConfig params = mFilledView.getLayoutConfig();
        params.height = mStarHeight;
        mFilledView.setLayoutConfig(params);
        mEmptyView.setLayoutConfig(params);
    }
}
